<template>
  <div class="demo-main">
    <div class="demo-title" style="margin: 50% 0px 16px">
      <h2>图书管理系统登录</h2>
    </div>
    <van-form :model="login" @submit="record">
      <van-cell-group inset>
        <van-field
          v-model="login.username"
          name="username"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="login.password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="success" native-type="submit">
          登录
        </van-button>
        <van-button
          style="margin-top: 16px"
          round
          block
          type="success"
          @click="register()"
        >
          注册
        </van-button>
      </div>
    </van-form>
  </div>
</template>
<script>
import { login } from "@/api/api";
export default {
  data() {
    return {
      login: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    record() {
      if (this.login.password == "" || this.login.username == "") {
        this.$Message.error("请输入账号或密码");
      } else {
        login({
          username: this.login.username,
          password: this.login.password,
        }).then((res) => {
          if (res == "") {
            this.$Message.error("账号密码错误");
          } else {
            localStorage.setItem("username", this.login.username);
            this.$Message.success("登录成功");
            setTimeout(() => {
              this.$router.push({
                name: "main",
              });
            }, 1000);
          }
        });
      }
    },
    register() {
      setTimeout(() => {
        this.$router.push({
          name: "register",
        });
      }, 1000);
    },
  },
};
</script>
<style>
.demo-main {
  width: 100%;
  height: 100%;
  background-image: url("@/assets/1.png");
  background-size: 100% 100%;
  background-position: center center;
  overflow: auto;
  background-repeat: no-repeat;
  position: fixed;
  line-height: 100%;
}
.demo-title {
  text-align: center;
}
</style>
